using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;

namespace Dracham.TileManagement
{
    public class Animation : IDisposable
    {
        private class Image
        {
            private Int32 x;

            public Int32 X
            {
                get { return x; }
                set { x = value; }
            }
            private Int32 y;

            public Int32 Y
            {
                get { return y; }
                set { y = value; }
            }

            public Image(Int32 x, Int32 y)
            {
                this.x = x;
                this.y = y;
            }
        }

        private Int32 x;
        private Int32 y;
        private Int32 changeSpeed;

        private List<Image> images;
        private Int32 currentImageNum = 0;
        private Double timeSinceLastImageChange = 0;
        private Texture2D texture;
        private Int32 rounds;
        private Int32 currentRound = 0;
        private Boolean disposed = false;

        public Boolean Disposed
        {
            get
            {
                return this.disposed;
            }
        }

        public Animation(String name, Int32 rx, Int32 ry, Int32 rounds)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("Data\\Animations\\" + name + ".xml");

            XmlNode animationNode = xDoc.GetElementsByTagName("Animation")[0];

            String source = animationNode.Attributes["Source"].Value;
            this.changeSpeed = Convert.ToInt32(animationNode.Attributes["ChangeSpeed"].Value);

            this.x = rx;
            this.y = ry;
            this.rounds = rounds;
            this.texture = Dracham.ContentManager.Load<Texture2D>(source);

            //
            // load images
            this.images = new List<Image>();
            foreach (XmlNode imageNode in animationNode.ChildNodes)
            {
                Int32 x = Convert.ToInt32(imageNode.Attributes["X"].Value);
                Int32 y = Convert.ToInt32(imageNode.Attributes["Y"].Value);

                images.Add(new Image(x, y));
            }
        }

        public void Update(GameTime gameTime)
        {
            if (disposed)
                return;

            timeSinceLastImageChange += gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timeSinceLastImageChange > changeSpeed)
            {
                timeSinceLastImageChange = 0;

                this.currentImageNum++;
                if (this.currentImageNum == this.images.Count)
                {
                    this.currentImageNum = 0;
                    this.currentRound++;

                    if (this.currentRound == rounds)
                    {
                        this.Dispose();
                    }
                }
            }
        }

        public void Render(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (disposed)
                return;

            Image i = this.images[this.currentImageNum];

            spriteBatch.Draw(
                texture,
                new Rectangle(this.x, this.y, Dracham.TileWidth, Dracham.TileHeight),
                new Rectangle(i.X * Dracham.TileWidth, i.Y * Dracham.TileHeight, Dracham.TileWidth, Dracham.TileHeight),
                Color.White
            );
        }

        #region IDisposable Members

        public void Dispose()
        {
            this.disposed = true;
        }

        #endregion
    }
}
